package app.client.file.copys;

import java.io.File;

public class FileBigCopy {
	
	private String srcFilePath;
	
	private String locFilePath;
	
	public String getSrcFilePath() {
		return srcFilePath;
	}

	public void setSrcFilePath(String srcFilePath) {
		this.srcFilePath = srcFilePath;
	}

	public String getLocFilePath() {
		return locFilePath;
	}

	public void setLocFilePath(String locFilePath) {
		this.locFilePath = locFilePath;
	}

	public void copy(){
		this.copyFile(new File(this.srcFilePath), new File(this.locFilePath));
	}

	
	public void copyFile(File srcFile,File locFile){
		
		String srcFolderPath = srcFile.getPath().replaceAll("\\\\", "/");
		String locFolderPath = locFile.getPath().replaceAll("\\\\", "/");
		
		File[] srcChilds = srcFile.listFiles();
		for(File srcChild:srcChilds){
			
			String srcName = srcChild.getName();
			File tmpFile = new File(locFolderPath+"/"+srcName);
			
			if(tmpFile.exists()){
				
				//文件夹处理
				if(srcChild.isDirectory()){
					this.copyFile(srcChild, tmpFile);
				}
				
				//文件处理
				else{
					
					long locLength = tmpFile.length();
					long srcLength = srcChild.length();
					
					if(srcLength>=locLength){
						
						tmpFile.delete();
						srcChild.renameTo(tmpFile);
					}else{
						System.out.println("保留文件："+srcChild.getPath());
					}
					
				}
				
			}else{
				//直接移动文件夹
				srcChild.renameTo(tmpFile);
			}
			
			
		}
		
	}
	
	private String formatPath(String filePath){
		return filePath.replaceAll("\\\\", "/");
	}
	
	public static void main(String[] args) {
		
		FileBigCopy bigFile = new FileBigCopy();
		/*bigFile.srcFilePath = "E:/My_Project/dataEmpire/WebRoot/file/txt";
		bigFile.locFilePath = "E:/VDownload/已完成";*/
		
		bigFile.srcFilePath = "G:/待合并";
		bigFile.locFilePath = "G:/已完成";
		
		bigFile.copy();
	}
}
